﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using ChiDao.Component.Utils;
using ChiDao.Business.Models;
using ChiDao.Business.Models.FlowProcessNew;
using ChiDao.Site.Models;
using ChiDao.Business.Data.Repositories.FlowProcessNew;
using System.Xml;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(IProcessNodeNewService))]
    internal class ProcessNodeNewService : IProcessNodeNewService
    {
        [Import]
        protected IProcessNewRepository ProcessNewRepository { get; set; }
        [Import]
        protected IProcessNodeNewRepository ProcessNodeNewRepository { get; set; }

        public List<ProcessNodeNewView> GetProcessNode(string processCode)
        {
            ProcessNew processNew = ProcessNewRepository.Entities.Where(p => p.ProcessCode == processCode).First();
            IQueryable<ProcessNodeNew> processNodeNewList = ProcessNodeNewRepository.Entities.Where(p => p.PID == processNew.ID);
            List<ProcessNodeNewView> processNodeNewViewList = new List<ProcessNodeNewView>();
            foreach (ProcessNodeNew processNodeNew in processNodeNewList)
            {
                processNodeNewViewList.Add(new ProcessNodeNewView()
                {
                    ID = processNodeNew.ID,
                    PID = processNodeNew.PID,
                    NodeCode = processNodeNew.NodeCode,
                    NodeName = processNodeNew.NodeName,
                    LocationX = processNodeNew.LocationX,
                    LocationY = processNodeNew.LocationY,
                    Width = processNodeNew.Width,
                    Height = processNodeNew.Height,
                    Shape = processNodeNew.Shape,
                    NodeXML = processNodeNew.NodeXML,
                    OrderNo = processNodeNew.OrderNo,
                    NodeType = processNodeNew.NodeType
                });
            }
            return processNodeNewViewList;
        }

        public ProcessNodeNewView GetProcessNodeByNodeCode(string processCode, bool isRequestNode, string nodeCode)
        {
            ProcessNew processNew = ProcessNewRepository.Entities.Where(p => p.ProcessCode == processCode).First();
            ProcessNodeNew processNodeNew = null;
            if (isRequestNode)
            {
                processNodeNew = ProcessNodeNewRepository.Entities.Where(p => p.PID == processNew.ID && p.NodeType == "0").First();
            }
            else
            {
                processNodeNew = ProcessNodeNewRepository.Entities.Where(p => p.PID == processNew.ID && p.NodeCode == nodeCode).First();
            }
            ProcessNodeNewView processNodeNewView = new ProcessNodeNewView()
            {
                ID = processNodeNew.ID,
                PID = processNodeNew.PID,
                NodeCode = processNodeNew.NodeCode,
                NodeName = processNodeNew.NodeName,
                LocationX = processNodeNew.LocationX,
                LocationY = processNodeNew.LocationY,
                Width = processNodeNew.Width,
                Height = processNodeNew.Height,
                Shape = processNodeNew.Shape,
                NodeXML = processNodeNew.NodeXML,
                OrderNo = processNodeNew.OrderNo,
                NodeType = processNodeNew.NodeType
            };
            return processNodeNewView;
        }
    }
}